package com.blb.marry.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.marry.entity.User;
import com.blb.marry.entity.VO.UserVO;
import com.blb.marry.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author lvtian
 * @date 2023/1/11 11:33
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    //进入登录页面的方法
    @RequestMapping("/toLogin")
    public String toRegister() {
        return "login";
    }


    //登录验证
    @ResponseBody
    @RequestMapping("/login")
    public String login(@RequestBody UserVO userVO, HttpSession session ){
        System.out.println(userVO);
        String code1 = (String)session.getAttribute("code");
        //判断验证码是否相同
        if(userVO.getCode() == null || !code1.equals(userVO.getCode())){
            //添加错误信息
            //跳转到登录页面r
            return "-1";
        }
        User user = userService.getOne(new QueryWrapper<User>().lambda().eq(User::getUserTel,userVO.getUserTel()).eq(User::getUserPassword, userVO.getUserPassword()));
        if(user == null){
            //添加错误信息
            //跳转到登录页面
            return "0";
        }else {
            session.setAttribute("user", user);
            return "1";
        }
    }


//    注册
    @ResponseBody
    @RequestMapping("/registerUser")
    public String registerUser(@RequestBody UserVO userVO, HttpSession session){
        User user = new User();
        user.setUserTel(userVO.getUserTel());
        user.setUserPassword(userVO.getUserPassword());
        if(userVO.getCode() != null){
            return "1";
        }
        boolean save = userService.save(user);
        if(save){
            return "1";
        }else {
            return "0";
        }
    }

    //通过id找到用户信息
    @RequestMapping("/findUser")
    public String findUser(HttpSession session ,Model model){
        User user = (User)session.getAttribute("user");
        User userById = userService.getById(user.getUserId());
        model.addAttribute("user",userById);
        return "forward:/personal_data.html";
    }


    //修改用户信息
}
